package com.huaxin.hxmoduleworkorder.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.huaxin.hxmoduleworkorder.models.InspectionOrderItem;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface InspectionOrderItemMapper extends BaseMapper<InspectionOrderItem> {
    /**
     * 根据配电房ID列表，统计每个配电房的巡检项总数和已完成数
     *
     * @param roomIds 配电房ID列表
     * @return List<Map < String, Object>>，包含 room_id, total, completed
     */
    @Select({
            "<script>",
            "SELECT",
            "  electrical_room_id,",
            "  COUNT(*) AS total,",
            "  SUM(CASE WHEN result_status IN ('normal', 'abnormal') THEN 1 ELSE 0 END) AS completed",
            "FROM inspection_order_item",
            "WHERE is_deleted = 0",
            "  AND inspection_order_id = #{inspectionOrderId}",
            "  AND electrical_room_id IN",
            "  <foreach item='id' collection='roomIds' open='(' separator=',' close=')'>",
            "    #{id}",
            "  </foreach>",
            "GROUP BY electrical_room_id",
            "</script>"
    })
    @Results({
            @Result(column = "electrical_room_id", property = "room_id"),
            @Result(column = "total", property = "total"),
            @Result(column = "completed", property = "completed")
    })
    List<Map<String, Object>> countItemStatsByRoomIds(@Param("roomIds") List<String> roomIds, @Param("inspectionOrderId") String inspectionOrderId);
}